package com.cosytek.farmmanager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cosytek.farmmanager.data.FarmSummary;
import com.cosytek.farmmanager.data.Main;

import java.util.ArrayList;

/**
 * Created by Happy on 2015/4/19.
 */
public class MainFragmentFarmAdapter extends BaseAdapter{

    private static final String TAG = "MainFragmentFarmAdapter";

    public MainFragmentFarmAdapter(Main main, LayoutInflater inflater) {
        viewInflater = inflater;
        this.main = main;
    }

    private final LayoutInflater viewInflater;

    private Main main;

    @Override
    public int getCount() {
        return main.getFarmSummaryCount();
    }

    @Override
    public Object getItem(int position) {
        FarmSummary fs = main.getFarmSummary(position);
        return fs;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            convertView = viewInflater.inflate(R.layout.frag_main_farm_list_item, null);
            vh = new ViewHolder();
            vh.viewFarmName = (TextView)convertView.findViewById(R.id.frag_main_farm_list_item_farm_name);
            vh.viewTaskCount = (TextView)convertView.findViewById(R.id.frag_main_farm_list_item_task_count);
            vh.viewPlanCount = (TextView)convertView.findViewById(R.id.frag_main_farm_list_item_plan_count);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder)convertView.getTag();
        }

        FarmSummary fs = main.getFarmSummary(position);
        vh.viewFarmName.setText(fs.getName());
        vh.viewTaskCount.setText(String.format("%s",fs.getTaskCount()));
        vh.viewPlanCount.setText(String.format("%s",fs.getPlanCount()));

        return convertView;
    }

    public void invalidate() {
        notifyDataSetChanged();
    }

    public void updateData(Main main) {
        this.main = main;
        invalidate();
    }

    private class ViewHolder {
        public TextView viewFarmName;
        public TextView viewPlanCount;
        public TextView viewTaskCount;
    }
}
